//
//  LCGlobalBaseViewController.m
//  lechuang
//
//  Created by Mr_hi on 2024/8/22.
//  Copyright © 2024 Mooyi. All rights reserved.
//

#import "LCGlobalBaseViewController.h"

#import "LCGlobalVideoViewController.h"
#import "LCGlobalLifeViewController.h"
#import "LCTravelListViewController.h"

#import "LCGlobalService.h"

#import "XWXAllXieYiController.h"

@interface LCGlobalBaseViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *videoBtn;
@property (weak, nonatomic) IBOutlet UIButton *travelBtn;

@property (weak, nonatomic) IBOutlet UIButton *lifeBtn;
@property (weak, nonatomic) IBOutlet UIButton *bookBtn;

@property (weak, nonatomic) IBOutlet UIScrollView *contentView;

@property (strong, nonatomic)NSMutableArray * linkArr;

@end

@implementation LCGlobalBaseViewController


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setMainUI];
    
    [self getClobalLinks];
    
}

- (void)setMainUI {
    
    self.contentView.width = ScreenW;
    self.contentView.contentSize = CGSizeMake(2*ScreenW, 0);
    
    self.videoBtn.selected = YES;
    self.videoBtn.backgroundColor = [UIColor colorWithHexString:@"#16A1E3"];
    
    self.travelBtn.selected = NO;
    self.travelBtn.backgroundColor = [UIColor colorWithHexString:@"#FFB46A"];
    
    LCGlobalVideoViewController * videoVC = [LCGlobalVideoViewController xx_instantiateFromStoryboardNamed:@"Global"];
    [self addChildViewController:videoVC];
    videoVC.view.frame = CGRectMake(0, 0, ScreenW, CGRectGetHeight(self.contentView.frame));
    [self.contentView addSubview:videoVC.view];
        
    LCTravelListViewController * travelVC = [LCTravelListViewController xx_instantiateFromStoryboardNamed:@"Global"];
    [self addChildViewController:travelVC];
    travelVC.view.frame = CGRectMake(ScreenW * 1, 0, ScreenW, CGRectGetHeight(self.contentView.frame));
    [self.contentView addSubview:travelVC.view];
}

- (IBAction)videoBtnAction:(UIButton *)sender {
    sender.selected = YES;
    sender.backgroundColor = [UIColor colorWithHexString:@"#16A1E3"];
    
    self.travelBtn.selected = NO;
    self.travelBtn.backgroundColor = [UIColor colorWithHexString:@"#FFB46A"];
    
    [self.contentView setContentOffset:CGPointMake(ScreenW * 0, 0) animated:YES];
}

- (IBAction)travelBtnAction:(UIButton *)sender {
    sender.selected = YES;
    sender.backgroundColor = [UIColor colorWithHexString:@"#16A1E3"];
    
    self.videoBtn.selected = NO;
    self.videoBtn.backgroundColor = [UIColor colorWithHexString:@"#FFB46A"];

    [self.contentView setContentOffset:CGPointMake(ScreenW * 1, 0) animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.contentView) {
        CGFloat offsetX = scrollView.contentOffset.x;
        NSInteger page = offsetX/ScreenW;
        if (page == 0) {
            self.videoBtn.selected = YES;
            self.videoBtn.backgroundColor = [UIColor colorWithHexString:@"#16A1E3"];

            self.travelBtn.selected = NO;
            self.travelBtn.backgroundColor = [UIColor colorWithHexString:@"#FFB46A"];
        } else if (page == 1) {
            self.travelBtn.selected = YES;
            self.travelBtn.backgroundColor = [UIColor colorWithHexString:@"#16A1E3"];
            
            self.videoBtn.selected = NO;
            self.videoBtn.backgroundColor = [UIColor colorWithHexString:@"#FFB46A"];
        }
    }
}


- (void)getClobalLinks {
    [LCGlobalService getGlobalLinkWithBlock:^(BOOL isSuccess, NSArray<LCCouponLinkModel *> *list) {
        if (isSuccess) {
            [self.linkArr removeAllObjects];
            [self.linkArr addObjectsFromArray:list];
            [self confogLinkBtns];
        }
    }];
}

- (NSMutableArray *)linkArr {
    if (!_linkArr) {
        _linkArr = [NSMutableArray array];
    }
    return _linkArr;
}

- (void)confogLinkBtns {
    if (self.linkArr.count > 0) {
        LCCouponLinkModel *model = [self.linkArr firstObject];
        [self.lifeBtn setTitle:model.linksname forState:UIControlStateNormal];
    }
    if (self.linkArr.count > 1) {
        LCCouponLinkModel *model = self.linkArr[1];
        [self.bookBtn setTitle:model.linksname forState:UIControlStateNormal];
    }
}

- (IBAction)lifeBtnAction:(UIButton *)sender {
    if (self.linkArr.count > 0) {
        LCCouponLinkModel *model = [self.linkArr firstObject];
        [self pushToWebVCWithModel:model];
    }
}

- (IBAction)bookBtnAction:(UIButton *)sender {
    if (self.linkArr.count > 1) {
        LCCouponLinkModel *model = self.linkArr[1];
        [self pushToWebVCWithModel:model];
    }
}

- (void)pushToWebVCWithModel:(LCCouponLinkModel *)model {
    XWXAllXieYiController *webVC = [XWXAllXieYiController new];
    webVC.tileStr = model.linksname;
    webVC.urlStr = model.linksurl;
    webVC.isPushFrom = YES;
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
}


@end
